var app = getApp()
Page({
  data: {
    payWay:1,
    orderstatus:'',
  },
  onLoad: function (e) {
    var that = this;
    app.setVersion(that);
  },
  onShow: function () {
    var that = this;
    that.getOrderInfo();
    that.requestPostType();
  },
  onUnload:function(){
    wx.removeStorage({
      key: 'submitOrder',
      success: function(res) {
        console.log("清楚订单信息成功");
      },
    })
  },
  getOrderInfo: function () {
    var that = this;
    wx.getStorage({
      key: 'submitOrder',
      success: function (res) {
        console.log('地址',res.data);
        var orderInfo = res.data;
        orderInfo.goodsData.map(val=>{
          val.price=val.price.toFixed(2)
        })
        if (!that.data.remarkExtra){
          var remarkExtra = orderInfo.remarkExtra;
          if (remarkExtra) {
            for (var i = 0; i < remarkExtra.length; i++) {
              remarkExtra[i].id = i;
              remarkExtra[i].value = '';
            }
            that.setData({
              remarkExtra: remarkExtra
            })
          }else{
            that.setData({
              remarkExtra: []
            })
          }
        }
        that.setData({
          orderInfo,
          addressInfo: res.data.address,
          cardNum: res.data.idcard,
          sumTotal: res.data.total,
          postTotal: res.data.postTotal,
          goodPostTotal: res.data.total
        })
      }
    })
  },
  // 付款方式配置
  requestPostType: function () {
    var that = this;
    wx.request({
      url: app.globalData.requestUrl,
      data: {
        map: 'applet_pay_cfg'
      },
      success: function (res) {
        console.log(res.data);
        if (res.data.ec == 200) {
          that.setData({
            payType: res.data.data,
            payWay: res.data.data[0].type
          })
        } else {
          // app.errorTip(that, res.data.em, 2000);
          that.setData({
            payType: [],
            payWay: 1
          })
        }
      },
      complete: function () {
      }
    });
  },
  payRadioChange: function (e) {
    this.setData({
      payWay: e.detail.value
    })
    console.log(this.data.payWay);
  },
  chooseAddress: function (e) {
    var that = this;
    var type = e.currentTarget.dataset.type;
    console.log(type);
    wx.navigateTo({
      url: '/pages/addressManage/addressManage?type=' + type
      // url: '../addressManage/addressManage?type=' + type
    })
  },
  nowPay: function () {
    var that = this;
    var payWay = that.data.payWay;
    console.log("订单状态" + that.data.orderstatus);
    var payWay = that.data.payWay;
    if (that.data.orderstatus == 'dzf') {
      if (payWay == 1) {
        that.orderPay();
      }
    } else {
      that.submitOrder();
    }
  },
  submitOrder: function () {
    var that = this;
    let remarkExtra = that.data.remarkExtra
    remarkExtra.map((val)=>{
      if(val.require == true) {
          if(val.value=='') {
            wx.showModal({
              title: '提示',
              content: '请输入'+val.name,
              showCancel: true
            })
          }
      }
    })
    app.getSubId(that, 'applet_order_confirm', 'toSubmitOrder');
  },
  toSubmitOrder: function () {
    var that = this;
    var type = that.data.orderInfo.needAddress;
    var tradetype = that.data.orderInfo.tradeType;
    let remarkExtra = that.data.remarkExtra
    var payWay = that.data.payWay;
    var data = {};
    data.map = 'applet_order_confirm';
    data.tid = that.data.orderInfo.tid;
    data.postFee = that.data.orderInfo.postTotal;
    data.payType = that.data.payWay;

    if (that.data.addressInfo.id) {
      data.addrid = that.data.addressInfo.id;
    } else {
      data.addrid = "";
    }
    if (type==1 && !data.addrid) {
      app.errorTip(that, "请选择收货地址", 2000);
      return;
    }
    for (var i = 0; i < remarkExtra.length; i++) {
      if (remarkExtra[i].require) {
        if (!remarkExtra[i].value) {
          if (remarkExtra[i].type == 'time' && remarkExtra[i].date && remarkExtra[i].dateval && remarkExtra[i].timeval) {
            remarkExtra[i].value = remarkExtra[i].dateval + ' ' + remarkExtra[i].timeval;
          }
          if (remarkExtra[i].type == 'time') {
            if (!remarkExtra[i].dateval) {
              app.errorTip(that, '请选择' + remarkExtra[i].name + '的日期', 2000);
              return;
            }
            if (!remarkExtra[i].timeval) {
              app.errorTip(that, '请选择' + remarkExtra[i].name + '的时间', 2000);
              return;
            }
          } else {
            app.errorTip(that, remarkExtra[i].name + '为必填项', 2000);
            return;
          }
        }
      }
    }
    data.remarkExtra = that.data.remarkExtra;
    console.log(data);
    //发起请求，获取列表列表
    wx.showLoading({
      title: '加载中',
    })
    wx.request({
      url: app.globalData.requestUrl,
      data: data,
      success: function (res) {
        if (res.data.ec == 200) {
          console.log(data);
          console.log(res.data.data);
          that.setData({
            orderstatus: res.data.data.status
          })
          if (res.data.data.status == 'dzf' && payWay == 1) {
            that.orderPay();
          } else {
            wx.redirectTo({
              url: '/pages/paySuccess/paySuccess?type=bargain&orderid=' + data.tid
            })
          }
        } else {
          console.log(res.data.em);
          app.errorTip(that, res.data.em, 2000);
        }
      },
      complete: function () {
        wx.hideLoading();
      }
    });
  },
  orderPay: function (e) {
    var that = this;
    var tradetype = that.data.orderInfo.tradeType;
    var data = {};
    //data.map = 'applet_order_pay';
    data.map = 'applet_order_pay_v2';
    data.tids = that.data.orderInfo.tid;
    wx.hideToast();
    //发起请求，获取列表列表
    wx.showLoading({
      title: '加载中',
    })
    wx.request({
      url: app.globalData.requestUrl,
      data: data,
      success: function (res) {
        if (res.data.ec == 200) {
          console.log(res.data.data);
          wx.requestPayment({
            'appId': res.data.data.appId,
            'timeStamp': res.data.data.timeStamp,
            'nonceStr': res.data.data.nonceStr,
            'package': res.data.data.package,
            'signType': res.data.data.signType,
            'paySign': res.data.data.paySign,
            'success': function (res) {
              wx.showModal({
                title: '提示',
                content: '支付成功',
                showCancel: false,
                success: function (res) {
                  if (res.confirm) {
                    wx.showLoading({
                      title: '加载中',
                    })
                    setTimeout(function () {
                      wx.hideLoading();
                      wx.redirectTo({
                        //  url: '/subpages/bargainOrderDetail/bargainOrderDetail?id=' + that.data.orderInfo.tid
                        url: '/subpages1/orderDetail/orderDetail?orderid=' + that.data.orderInfo.tid
                      })
                    }, 1000)
                  }
                }
              })
            },
            'fail': function (res) {
              console.log(res);
            }
          });
        } else {
          app.errorTip(that, res.data.em, 2000);
        }
      },
      complete: function () {
        wx.hideLoading();
      }
    });
  },
  // 监控数据改变
  dataChange: function (e) {
    var that = this;
    var remarkExtra = that.data.remarkExtra;
    var id = e.currentTarget.dataset.id;
    var type = e.currentTarget.dataset.type;
    var curVal = e.detail.value;
    for (var i = 0; i < remarkExtra.length; i++) {
      if (remarkExtra[i].id == id) {
        if (remarkExtra[i].type == 'time' && remarkExtra[i].date){
          console.log("日期时间必填");
          remarkExtra[i][type] = curVal;
          if (remarkExtra[i].dateval && remarkExtra[i].timeval){
            remarkExtra[i].value = remarkExtra[i].dateval + ' ' + remarkExtra[i].timeval;
          }
        }else{
          remarkExtra[i].value = curVal;
        }
      }
    }
    that.setData({
      remarkExtra: remarkExtra
    })
    // console.log(that.data.remarkExtra);
  },
   // 上传单图到服务器
   upload_file: function (temPath, id) {
    var that = this;
    var remarkExtra = that.data.remarkExtra;
    wx.showLoading({
      title: '上传中',
    })
    wx.uploadFile({
      url: app.globalData.requestUrl + '?&map=applet_img_upload',
      filePath: temPath,
      name: 'image',
      success: function (res) {
        var data = JSON.parse(res.data);
        var realpath = app.globalData.domin + data.data.path;
        if (data.ec == 200) {
          for (var i = 0; i < remarkExtra.length; i++) {
            if (remarkExtra[i].id == id) {
              remarkExtra[i].img = realpath;
              remarkExtra[i].value = data.data.path;
            }
          }
          that.setData({
            remarkExtra: remarkExtra
          })
          console.log(that.data.remarkExtra);
        } else {
          wx.showModal({
            title: '提示',
            content: data.em,
            showCancel: false
          });
        }
      },
      complete: function () {
        wx.hideLoading();
      }
    });
  },
})
